package hot100.logos81To90;

/**
 * @author logos
 * date 2025/7/18 18:54
 * @version v1.0
 * @Package hot100.logos81To90
 */
public class logos88 {

    /**
     * 88. 乘积最大子数组
     * https://leetcode.cn/problems/maximum-product-subarray/?envType=study-plan-v2&envId=top-100-liked
     */

    class Solution {
        public int maxProduct(int[] nums) {
            int n = nums.length;
            int max = nums[0];
            int min = nums[0];
            int ans = nums[0];
            int curmin = 0;
            int curmax = 0;
            for (int i = 1; i < n; i++) {
                curmax = Math.max(nums[i], Math.max(nums[i] * max, min * nums[i]));
                curmin = Math.min(nums[i], Math.min(nums[i] * min, max * nums[i]));
                max = curmax;
                min = curmin;
                ans = Math.max(ans, max);
            }
            return ans;
        }
    }
}
